/*
Uninclude: Tool for detecting unused #included files in C and C++ code.
    Copyright (C) 2012  Daniel Gutson, FuDePAN

    Uninclude is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Uninclude is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Uninclude.  If not, see <http://www.gnu.org/licenses/>.

    This is a CPP testing file.
*/

#include "uninclude/cpp_parser.h"

class UnincluderListener : public CppParser::Listener
{
public:
    struct Listener
    {
        virtual void setBase(const string& baseFile) = 0;
        virtual void report(const string& includer, const string& name, size_t line) = 0;
        virtual ~Listener(){}
    };
private:
    Listener* const listener;
    size_t systemDepth;
    size_t lastLine;
    string lastFile;

    virtual void setRoot(const string& rootName, size_t line)
    {
        if (lastFile.empty())
        {
            lastFile = rootName;
            lastLine = line;
            listener->setBase(rootName);
        }
    }

    virtual void pushInclude(const string& newName, size_t line, bool systemInclude)
    {
        if (systemInclude)
            ++systemDepth;

        if (systemDepth <= 1)
            listener->report(lastFile, newName, lastLine);

        lastLine = line;
        lastFile = newName;
    }

    virtual void popInclude(const string& oldName, size_t line)
    {
        if (systemDepth > 0)
            --systemDepth;

        lastLine = line;
        lastFile = oldName;
    }
public:
    UnincluderListener(Listener* listener) : listener(listener), systemDepth(0), lastLine(1){}
};

class TestListener: public UnincluderListener::Listener
{
    virtual void setBase(const string& baseFile)
    {
        cout << "base:\t" << baseFile << endl;
    }

    virtual void report(const string& includer, const string& name, size_t line)
    {
        cout << line << "\t" << includer << "\t->\t" << name << endl;
    }
};

